<?php
class Malbum extends CI_Model{
	function __construct(){
		parent::__construct();
	}
	function initAlbum($uid){
		$name = "默认专辑";
		$sql = " select * from tbalbum
				where uid = ? and name = ?";
		$query = $this->db->query($sql,array($uid,"$name"));
		$exists = false;
		foreach ($query->result() as $row){
			$exists = true;
		}
		if($exists == false){
			$sql = "insert into tbalbum(uid,name,`desc`,createtime)
					values(?,?,'',now())";
			$this->db->query($sql,array("$uid","$name"));
		}		
	}
	function checkExistsName($uid,$name){
		$sql = "select *
				from tbalbum
				where uid = ? and name = ?";
		$query = $this->db->query($sql,array("$uid","$name"));
		foreach ($query->result() as $row){
			return true;
		}
		return false;
	}
	
	function saveAlbumName($uid,$name){
		$sql = "insert into tbalbum(uid,name,`desc`,createtime) values(?,?,'',now())";
		$this->db->query($sql,array("$uid","$name"));
		return $this->db->affected_rows();
	}

    /**
     * 加载相册
     * @param $uid
     * @return mixed
     */
    function loadAlbums($uid){
		$sql = "select *
				from tbalbum
				where uid=? and status=1
				order by id";
		$query = $this->db->query($sql,array("$uid"));
        $dataAlbum = $query->result();
        if($query->num_rows() > 0){
            $item = array();
            foreach($dataAlbum as $value)
            {
                $item[] = $value->id;
            }


            $dataPic = $this->Malbumdetail->getAlbumDetail($item);


            foreach($dataAlbum as &$value){
                $value->url = isset($dataPic[$value->id]) ? $dataPic[$value->id] : '';
            }


            return $dataAlbum;
        }
	}

    /**
     * 检测是否有创建相册
     * @param $uid
     */
    function checkCreateAlbum($uid)
    {
        $sql = "select *
				from tbalbumdetail
				where uid=?";
        $query = $this->db->query($sql,array($uid));
        $exists = false;
        if($query->num_rows > 0){
            $exists = true;
        }
        return $exists;
    }

}
?>